<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div class="nav-left" @mouseover="isShow = true" @mouseout="changeShow">
        <h2 class="all">全部商品分类</h2>
        <div class="sort" v-if="isShow">
          <div class="all-sort-list2" @click="toSearch($event)">
            <!-- 一级分类 -->
            <div class="item" v-for="cate1 in categoryList.splice(1, 15)" :key="cate1.categoryId">
              <h3>
                <a data-level="1" :data-id="cate1.categoryId">{{ cate1.categoryName }}</a>
              </h3>
              <!-- 二级分类 -->
              <div class="item-list clearfix">
                <div class="subitem" v-for="cate2 in cate1.categoryChild" :key="cate2.categoryId">
                  <dl class="fore">
                    <dt>
                      <a data-level="2" :data-id="cate2.categoryId">{{ cate2.categoryName }}</a>
                    </dt>
                    <dd>
                      <em v-for="cate3 in cate2.categoryChild" :key="cate3.categoryId">
                        <a data-level="3" :data-id="cate3.categoryId">{{ cate3.categoryName }}</a>
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reqCategoryList } from '@/api/home'
import type { ICategoryListItem } from '@/types/home';
import { onMounted, ref } from 'vue'
import useHomeStore from '@/stores/homeStore'
import { storeToRefs } from 'pinia';
import router from '@/router';
import { useRoute } from 'vue-router';

//实例化store
const homeStore = useHomeStore()
//解构store
const { cate } = storeToRefs(homeStore)

const route = useRoute()

//三级分类是否展示
const isShow = ref( route.path === '/home' ? true : false)


//三级分类列表数据
const categoryList = ref<ICategoryListItem[]>([])

//首页展示三级分类导航，其他页面隐藏
//获取当前路由地址，判断是否为首页


//三级分类移入移出动态展示
const changeShow = ()=>{
  if(route.path === '/home' || route.path === '/') return
  isShow.value = !isShow.value
}



//跳转至搜索页
const toSearch = (e: any) => {
  //获取分类和id
  const { id, level } = e.target.dataset
  //获取分类名
  const categoryName = e.target.innerHTML

  //获取路由参数
  const query = route.query


  // console.log(route)

  //判断是否存在id，不存在id不进行跳转
  if (id) {
    //跳转页面
    router.push({
      path: '/search',
      query: {
        ...query,
        [`category${level}Id`]: id,
        categoryName
      }
    })
  }
}


//获取三级分类列表
const getCategoryList = async () => {
  const res = await reqCategoryList()
  // console.log(res.data)

  if (res.code === 200) {
    //更新状态
    categoryList.value = res.data

    //将数据存储到store
    cate.value = res.data as any
  }

}

onMounted(() => {
  //进入页面，发送三级分类列表请求
  getCategoryList()
})
</script>

<script lang="ts">
export default {
  name: 'TypeNav'
}
</script>

<style lang="scss" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      cursor: pointer;
    }


    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;
            cursor: pointer;

            a {
              color: #333;
            }
          }

          h3:hover {
            background-color: #e1251b;

            a {
              color: white !important
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 20px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 590px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>